package org.wattdepot.cli;

import java.io.Console;
import java.util.HashMap;
import java.util.Map; //import java.util.Scanner;
import org.wattdepot.cli.command.WattDepotCarbonContentCommand;
import org.wattdepot.cli.command.WattDepotChartCommand;
import org.wattdepot.cli.command.WattDepotCommand;
import org.wattdepot.cli.command.WattDepotFuelTypesCommand;
import org.wattdepot.cli.command.WattDepotHelpCommand;
import org.wattdepot.cli.command.WattDepotListDailySensordataCommand;
import org.wattdepot.cli.command.WattDepotListPowerCommand;
import org.wattdepot.cli.command.WattDepotListPowerStatsCommand;
import org.wattdepot.cli.command.WattDepotListSensordataCommand;
import org.wattdepot.cli.command.WattDepotListSourcesCommand;
import org.wattdepot.cli.command.WattDepotListSummaryCommand;
import org.wattdepot.cli.command.WattDepotListTotalCommand;
import org.wattdepot.cli.command.WattDepotQuitCommand;
import org.wattdepot.cli.command.WattDepotTotalPowerCommand;
import org.wattdepot.client.WattDepotClient;

/**
 * Contains the main method for the Command Line Application for Wattdepot.
 * 
 * @author Alex Kan and Kelvin Green
 * @version 1.0
 */
public class CommandLineInterface {
  /**
   * Interface to operating system console.
   */
  private Console console;

  private Map<String, WattDepotCommand> map;

  final String cr = System.getProperty("line.separator");

  static WattDepotClient client = null;

  /**
   * The main program to connects to the WattDepot Server.
   * 
   * @param args holds the address of the wattdepot server.
   */
  public static void main(String[] args) {
    if (args.length < 1 || args.length > 1) {
      // http://server.wattdepot.org:8182/wattdepot/
      System.out.println("WattDepot Server address must be an argument.");
      return;
    }
    WattDepotCommand.connect(args[0]);
    CommandLineInterface cli = new CommandLineInterface();
    cli.commandPrompt();

  }

  /**
   * Public constructor that initializes the CommandeLine class and creates command map.
   * 
   */
  public CommandLineInterface() {
    console = System.console();
    map = createHashMap();
  }

  /**
   * Displays the command prompt to the user and sends the input to the command processor.
   */
  public void commandPrompt() {
    String output = prompt();
    if (0 == output.compareTo("quit")) {
      System.out.println("Exiting CLI");
    }
    else {
      System.out.println(output);
      commandPrompt();
    }
  }

  /**
   * Processes input from the user and returns output.
   * @return output from method.
   */
  private String prompt() {
    String cmd = console.readLine("> ");
    String[] commandArray = cmd.split(" ");
    String command = commandArray[0];
    WattDepotCommand commandInstance = map.get(command);

    if (commandInstance == null) {
      return "Not a valid command." + cr;
    }
    else {
      return map.get(command).doCommand(commandArray);
    }
  }

  /**
   * Test the command prompt.
   * 
   * @param password holds code to authenticate testers.
   * @param cmd holds command input by user.
   * @return output from method.
   */
  public String promptTester(String password, String cmd) {
    if (0 == password.compareTo("tester")) {
      String[] commandArray = cmd.split(" ");
      String command = commandArray[0];
      WattDepotCommand commandInstance = map.get(command);

      if (commandInstance == null) {
        return "Not a valid command." + cr;
      }
      else {
        return map.get(command).doCommand(commandArray);
      }
    }
    return "Incorrect password.";
  }

  /**
   * Creates a Map of all the WattDepotCommands.
   * 
   * @return a Map containing all the WattDepotCommands.
   */
  private Map<String, WattDepotCommand> createHashMap() {
    Map<String, WattDepotCommand> map = new HashMap<String, WattDepotCommand>();
    map.put("help", new WattDepotHelpCommand());
    map.put("quit", new WattDepotQuitCommand());
    map.put("sources", new WattDepotListSourcesCommand());
    map.put("summary", new WattDepotListSummaryCommand());
    map.put("sensordata", new WattDepotListSensordataCommand());
    map.put("dailysensordata", new WattDepotListDailySensordataCommand());
    map.put("power", new WattDepotListPowerCommand());
    map.put("powerstats", new WattDepotListPowerStatsCommand());
    map.put("total", new WattDepotListTotalCommand());
    map.put("chart", new WattDepotChartCommand());
    map.put("fueltypes", new WattDepotFuelTypesCommand());
    map.put("totalpower", new WattDepotTotalPowerCommand());
    map.put("carboncontent", new WattDepotCarbonContentCommand());
    return map;
  }

}
